package com.qinsoft.frame.messages;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Date;

import com.qinsoft.frame.utils.DateUtils;

/** 
 * WriteMessage类描述：写日志类
 */
public class WriteMessage
{

    /**
     * 写错误日志
     * @param className：产生错误的类和方法名称
     * @param e :错误对象
     */
    public static void WriteErrorLog(String className, Exception e)
    {
        try
        {
            System.out.println("Error:" + new Date().toString() + " " + className);
            System.out.println(e.getMessage());
            StringBuilder errorMessage = new StringBuilder();
            errorMessage.append("Error:").append(DateUtils.getDate()).append(" ").append(className).append("\n");
            errorMessage.append("Detail:").append(e.getMessage()).append("\n");
            WriteError(errorMessage);
        }
        catch (Exception ex)
        {
            System.out.println("Error:com.smart.frame.messages.WriteMessage.WriteErrorLog()");
        }
    }

    /**
     * WriteError方法描述：
     * @param: 
     * @return:
     */
    private static void WriteError(StringBuilder errorMessage)
    {
        try
        {
            String time = DateUtils.getDate();
            String path = Thread.currentThread().getContextClassLoader().getResource("").getPath().split("WebRoot")[0]+"\\log\\log_"+ time +".txt";
            File file = new File(path);
            if(file.length()>5*1024){
                file = new File(path);    
            }
            OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file,true),"UTF-8");
            //BufferedWriter writer = new BufferedWriter(new FileWriter(file,true));
            writer.append(errorMessage);
            writer.close();
        }
        catch (IOException e)
        {
            System.out.println("Error:com.smart.frame.messages.WriteMessage.WriteError()");
        }
    }
}
